package com.example.park.controller;

import com.example.common.utils.R;
import com.example.park.entity.ParkingEntity;
import com.example.park.entity.UserEntity;
import com.example.park.entityVO.ParkingDimVO;
import com.example.park.entityVO.ParkingVO;
import com.example.park.service.ParkingService;
import com.example.park.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import com.example.park.entity.Error;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(tags = "管理员接口")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private ParkingService parkingService;

    @Autowired
    private UserService userService;

    /**
     * 查找所有用户
     * @param map
     * @return
     */
    @ApiOperation("查找所有用户")
    @RequestMapping(value = "/listByParam", method = RequestMethod.POST)
    public R listByParam(@RequestBody Map<String, String> map){
        String param = map.get("param");
        List<UserEntity> user = userService.queryByDim(param);
        return R.ok().put("list", user);
    }

    /**
     * 添加管理员用户
     * @param user
     * @return
     */
    @ApiOperation("添加管理员用户")
    @RequestMapping(value = "/adminRegister", method = RequestMethod.POST)
    public R adminRegister(@RequestBody UserEntity user){
        user.setState(0);
        if (userService.insert(user)) {
            return R.ok("添加成功！");
        }
        return R.error("添加失败！");
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @ApiOperation("更新用户")
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public R updateUser(@RequestBody UserEntity user){
        if (userService.update(user)) {
            return R.ok("更新成功！");
        }
        return R.error("更新失败！");
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @ApiOperation("删除用户")
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public R deleteUser(Integer id){
        if (userService.adminDelete(id)) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }

    /**
     * 车位主绑定信息 , uId为1时查找未绑定, 为2时查找已绑定
     * @param vo
     * @return
     */
    @ApiOperation("车位主绑定信息")
    @RequestMapping(value = "/parkingList", method = RequestMethod.POST)
    public R parkingList(@RequestBody ParkingDimVO vo){
        List<ParkingVO> vos = parkingService.queryVOByDim(vo);
        return R.ok().put("list", vos);
    }

    /**
     * 解绑信息
     * @param id
     * @return
     */
    @ApiOperation("解绑信息")
    @RequestMapping(value = "/unbindParking", method = RequestMethod.POST)
    public R unbindParking(Integer id){
        if (parkingService.unbind(id)) {
            return R.ok("解绑成功！");
        }
        return R.error("解绑失败！");
    }

    /**
     * 添加车位信息
     * @param parkingEntity
     * @return
     */
    @ApiOperation("添加车位信息")
    @RequestMapping(value = "/addPark", method = RequestMethod.POST)
    public R addPark(@Valid @RequestBody ParkingEntity parkingEntity) {
        if (parkingService.insert(parkingEntity)) {
            return R.ok("添加成功！");
        }
        return R.error("添加失败！");
    }

    /**
     * 修改车位信息
     * @param parkingEntity
     * @return
     */
    @ApiOperation("修改车位信息")
    @RequestMapping(value = "/updatePark", method = RequestMethod.POST)
    public R updatePark(@RequestBody ParkingEntity parkingEntity) {
        if (parkingService.update(parkingEntity)) {
            return R.ok("修改成功！");
        }
        return R.error("修改失败！");
    }

    /**
     * 删除车位信息
     * @param id
     * @return
     */
    @ApiOperation("删除车位信息")
    @RequestMapping(value = "/deletePark", method = RequestMethod.POST)
    public R deletePark(Integer id) {
        if (parkingService.delete(id)) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Error exceptionHander(HttpServletRequest req, MethodArgumentNotValidException e){
        List<ObjectError> errorList=e.getBindingResult().getAllErrors();
        String error="";
        for (ObjectError objectError : errorList) {
            error=objectError.getDefaultMessage();
        }
        Error ew=new Error("0001",error);
        return ew;
    }

}
